public class Test {
    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode head1 = l1;
            ListNode head2 = l2;
            int t = 0;
            int sum = 0;
            ListNode newHead = new ListNode(-1);
            ListNode cur = newHead;
            while(head1!=null&&head2!=null){
                sum = (head1.val+head2.val+t);
                ListNode node = new ListNode(sum%10);
                t = sum/10;
                cur.next = node;
                cur = node;
                head1 = head1.next;
                head2 = head2.next;
            }
            while(head1!=null){
                sum = (head1.val+t);
                ListNode node = new ListNode(sum%10);
                t = sum/10;
                cur.next = node;
                cur = node;
                head1 = head1.next;
            }
            while(head2!=null){
                sum = (head2.val+t);
                ListNode node = new ListNode(sum%10);
                t = sum/10;
                cur.next = node;
                cur = node;
                head2 = head2.next;
            }
            if(t!=0){
                ListNode node = new ListNode(t);
                cur.next = node;
                cur = node;
            }
            return newHead.next;
        }
    }
}